const { list, add, find } = require('./data/rooms')
const Mock = require('mockjs')

const base = '/userserver'

module.exports = [{
  url: `${base}/rooms`,
  type: 'post',
  response: config => {
    add(config.body)
    return { success: true }
  }
}, {
  url: `${base}/rooms/total`,
  type: 'get',
  response: config => {
    return { success: true, data: Mock.Random.integer(800, 1000) }
  }
}, {
  url: `${base}/rooms`,
  type: 'get',
  response: config => {
    const { page, limit, name, orgUnitId } = config.query
    return list(page, limit, name, orgUnitId)
  }
}, {
  url: `${base}/rooms/[1-9][0-9]*`,
  type: 'put',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    const r = find(id)
    if (r) {
      const { name } = config.body
      r.name = name
      return { success: true }
    } else {
      return { success: false, message: '记录不存在' }
    }
  }
}]
